<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of fx_paginator
 *
 * @author anhdt
 */
class fx_paginator {

    var $_current_index = 0;
    var $_param = 'page';
    var $_collection = array();
    var $_max_page    = 5;
    var $_max_record = 5;
    var $_tmp_page = array();
    var $_url = '';

    public function __construct($_collection = array(),
        $_max_record = 5,
        $_max_page = 5,
        $_index = 0,
        $_param = 'page') {
        $this->set_collection($_collection);
        $this->set_max_page($_max_record);
        $this->set_max_page($_max_page);
        $this->set_current_index($_index);
        $this->set_param_name($_param);
    }

    public function set_max_record($_num_records) {
        $this->_max_record = (int)$_num_records;
        return $this;
    }

    public function get_max_record() {
        return (int)$this->_max_record;
    }

    public function set_max_page($_num_pages) {
        $this->_max_page = (int)$_num_pages;
        return $this;
    }

    public function get_max_page() {
        return (int)$this->_max_page;
    }

    public function count_page() {
        $_total_records = count($this->_collection);
        if (count($_total_records) == 0)
            return 0;
        if ($this->_max_record <= 0) {
            $this->_max_record = 1;
            return $_total_records;
        }
        $_num_pages = $_total_records / $this->_max_record;
        if (is_integer($_num_pages)) {
            return $_num_pages;
        } else {
            return floor($_num_pages + 1) ;
        }
    }

    public function set_current_index($_index) {
        $this->_current_index = (int)$_index;
        return $this;
    }

    public function get_current_index() {
        return (int)$this->_current_index;
    }

    public function getCollection() {
        return (array)$this->_collection;
    }

    public function set_collection($_collection = array()) {
        if (!is_array($_collection)) {
            return $this;
        }
        $this->_collection = $_collection;
        return $this;
    }

    public function get_tmp_page() {
        return (array)$this->_tmp_page;
    }

    public function set_url($_url = '') {
        if (!is_string($_url) || empty($_url)) {
            return $this;
        }
        $this->_url = $_url;
        return $this;
    }

    public function get_url($_index = 1) {
        return (string)$this->_url . '?' . $this->_param . '=' . $_index;
    }

    public function get_concat_url($_str) {
        return (string)$this->_url . $_str;
    }

    public function get_page($_index = 0) {
        if ($this->count_page() == 0) {
            return array();
        }
        if (empty($_index)) {
            $_index = $this->_current_index;
        }
        $_max_record = $this->get_max_record();
        $this->_tmp_page = array();
        for ($i = ($_index * $_max_record); $i < (($_index + 1) * $_max_record); $i++) {
            if (isset($this->_collection[$i]))
                $this->_tmp_page[] = $this->_collection[$i];
        }
        return $this->_tmp_page;
    }

    public function set_param_name($_param = '') {
        if (!is_string($_param) || empty($_param)) {
            return $this;
        }
        $this->_param = $_param;
        return $this;
    }

    public function get_param_name() {
        return (string)$this->_param;
    }

    public function has_page($page_index) {
        $_pages = $this->count_page();
        if ($page_index > $_pages) {
            return FALSE;
        }
        return TRUE;
    }

    public function showLinks($_wrapper_id = '') {
        $_index = CI_Input::getParam($this->_param);
        ?>

<div class="paginator_links" <?php if (!empty($_wrapper_id)) echo "id=\"$_wrapper_id\""?>>
    <ul>

        <!-- FIRST BUTTON -->
        <li>
            <a href="<?php echo $this->get_url() ?>">First</a>
        </li>


        <!-- PREVIOUS BUTTON -->
        <?php if ($_index <= 1) :?>
        <li>
            <a href="<?php echo $this->get_url(1) ?>">
                <
            </a>
        </li>
        <?php else :?>
        <li>
            <a href="<?php echo $this->get_url($_index - 1) ?>">
                <
            </a>
        </li>
        <?php endif; ?>


        <!-- NUMBER BUTTON -->
                <?php  for ($_page = 1; $_page <= $this->count_page(); $_page++):?>
        <li>
            <a href="<?php echo $this->get_url($_page) ?>"><?php echo $_page ?></a>
        </li>
                <?php endfor; ?>


        <!-- NEXT BUTTON -->
        <?php if (!$this->has_page($_index + 1)) :?>
        <li>
            <a href="<?php echo $this->get_url($this->count_page()) ?>">
                >
            </a>
        </li>
        <?php else :?>
        <li>
            <a href="<?php echo $this->get_url($_index + 1) ?>">
                >
            </a>
        </li>
        <?php endif; ?>
        
        <!-- LAST BUTTON -->
        <li>
            <a href="<?php echo $this->get_url($this->count_page()) ?>">
                Last
            </a>
        </li>
        
    </ul>
</div>
    <?php
    }
}
?>
